﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Kaidou.Util;
using Kaidou.Bll;

public partial class master_master_branch_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if (!loginModel.isOperator() && !loginModel.isJimukyoku())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel != null && loginModel.isOperator())
        {
            updateButton.Visible = true;
            deleteButton.Visible = true;
        }
        

        if (!IsPostBack)
        {
            string area = Resources.Resource.area;
            string[] areaArray = area.Split(',');
            for (int i = 0; i < areaArray.Length; i++)
            {
                ListItem item = new ListItem();
                item.Value = (i + 1) + "";
                item.Text = areaArray[i];
                office_pref_code.Items.Insert(i + 1, item);
            }

            MST_Union_BranchPersister dao = new MST_Union_BranchPersister();
            mst_union_branch M = dao.getDetail(Request.QueryString["unioncode"].ToString(), int.Parse(Request.QueryString["branchcode"].ToString()));
            Session["mstunionbranch"] = M;

            MST_UnionPersister dao2 = new MST_UnionPersister();
            MstUnion parent=dao2.getDetail(M.Id.unioncode);
            union_name.Text = parent.OfficeName;
            union_code.Text = M.Id.unioncode;
            branch_code.Text =StringUtil.getBranchCodeNumberFormat(  M.Id.branchcode);
            office_address.Text = M.officeaddress;
            office_city.Text = M.officecity;
            office_email.Text = M.officeemail;
            office_fax.Text = M.officefax;
            office_kana.Text = M.officekana;
            office_name.Text = M.officename;
            office_pref_code.SelectedIndex = M.officeprefcode;
            office_tel.Text = M.officetel;
            if (M.officezipno != null && M.officezipno.Length > 0)
            {
                office_zip_no1.Text = M.officezipno.Substring(0, 3);
                office_zip_no2.Text = M.officezipno.Substring(3, 4);
            }
            tanto_kana.Text = M.tantokana;
            tanto_name.Text = M.tantoname;
            password.Text = M.password;
        }
    }

    protected void delete(object sender, EventArgs e)
    {

        mst_union_branch M = (mst_union_branch)Session["mstunionbranch"];

        M.upddate = System.DateTime.Now;
        M.updunioncode = (string)Session["Login_Union_code"];
        M.updbranchcode = (short)Session["Login_Branch_code"];
        M.updfuncid = "deleteBranch()";
        M.unionsts = StringUtil.STATUS_DELETE;
        Session["mstunionbranch"] = M;
        Response.Redirect("master_branch_delete_conf.aspx");

    }

    protected void update(object sender, EventArgs e)
    {
        mst_union_branch M = (mst_union_branch)Session["mstunionbranch"];
        mst_union_branchId MID = new mst_union_branchId();
        if (!ParameterCheckUtility.isNumber(branch_code.Text.Trim()))
        {
            MID.branchcode = -1;
        }
        else
        {
            MID.branchcode = branch_code.Text.Trim().Length == 0 ? (short)(-1) : short.Parse(branch_code.Text);
        }
        MID.unioncode = union_code.Text;
        M.Id = MID;
        M.officeaddress = office_address.Text;
        M.officecity = office_city.Text;
        M.officeemail = office_email.Text;
        M.officefax = office_fax.Text;
        M.officekana = office_kana.Text;
        M.officename = office_name.Text;
        M.officeprefcode = short.Parse(office_pref_code.Text);
        M.officetel = office_tel.Text;
        M.officezipno = office_zip_no1.Text + office_zip_no2.Text;
        M.officezipno1 = office_zip_no1.Text;
        M.officezipno2 = office_zip_no2.Text;
        M.password = password.Text;
        M.regdate = System.DateTime.Now;
        M.tantokana = tanto_kana.Text;
        M.tantoname = tanto_name.Text;

        M.upddate = System.DateTime.Now;
        M.updunioncode = (string)Session["Login_Union_code"];
        M.updbranchcode = (short)Session["Login_Branch_code"];

        //本店の情報を取得
        MST_UnionPersister dao = new MST_UnionPersister();
        MstUnion parentUnion = dao.getDetail(M.Id.unioncode);
        //本店の区分を支店にコピーする
        M.userclss = parentUnion == null ? null : parentUnion.UserClss;

        M.updfuncid = "updateBranch()";

        //検証
        MST_Union_BranchManager validation = new MST_Union_BranchManager();
        List<string> errorCodeList = validation.checkInsertOrUpdateBranch(M, Resources.Resource.ResourceManager, false);
        msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

        if (errorCodeList.Count == 0)
        {
            Session["mstunionbranch"] = M;
            Response.Redirect("master_branch_update_conf.aspx");
        }




    }

    protected void goback(object sender, EventArgs e)
    {
        Session["back"] = 1;
        Response.Redirect("master_branch_search.aspx");

    }

}
